<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/5/28
 * Time: 17:47
 */

namespace App\Obersvers;


use App\Models\CreditsExchangeRecord;
use App\Models\CreditsRecod;
use App\Models\MerchantTicket;

class CreditsExchangeObersver
{
    public function created(CreditsExchangeRecord $creditsExchangeRecord)
    {
        $credits_record_data['id_wxuser_mcard'] = $creditsExchangeRecord->id_wxuser_mcard;
        $insert_data['id_merchant'] = $creditsExchangeRecord->id_merchant;
        $credits_record_data['value'] = $creditsExchangeRecord->value;
        $credits_record_data['table_type'] = 'credits_exchange_record';
        $credits_record_data['table_id'] = $creditsExchangeRecord->id;
        $credits_record_data['title'] = '积分兑换';
        $credits_record_data['desc'] = '兑换扣积分';
        $time = now()->toDateTimeString();
        $credits_record_data['created_at'] = $time;
        $credits_record_data['updated_at'] = $time;

        //用户-券
        if($creditsExchangeRecord->id_merchant_ticket){
            $ticket = MerchantTicket::where('id_merchant_ticket',$creditsExchangeRecord->id_merchant_ticket)->where('is_delete','N')->first();
            if($ticket){
                \DB::table('wxcard_tickets')->insert([
                    'id_wxuser_mcard'=>$creditsExchangeRecord->id_wxuser_mcard,
                    'id_merchant_ticket'=>$creditsExchangeRecord->id_merchant_ticket,
                    'id_merchant'=>$creditsExchangeRecord->id_merchant,
                    'desc'=>'积分兑换',
                    'status'=>1,
                    'table_type'=>'credits_exchange_record',
                    'table_id'=>$creditsExchangeRecord->id,
                    'proof_expiry_time'=>$creditsExchangeRecord->proof_expiry_time,
                    'created_at'=>$time,
                    'updated_at'=>$time,
                ]);
            }
        }

        //积分记录
        CreditsRecod::create($credits_record_data);
    }
}